package com.atguigu.srb.core.controller.api;


import com.atguigu.common.result.R;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.pojo.vo.UserBindVO;
import com.atguigu.srb.core.service.UserBindService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 前端控制器
 * </p>
 *
 * @author Bright
 * @since 2022-04-09
 */
@Slf4j
@Api(tags = "用户绑定")
@RestController
@RequestMapping("/api/core/userBind")
public class UserBindController {

    @Resource
    UserBindService userBindService;


    @ApiOperation(value = "用户提交绑定数据")
    @PostMapping("/auth/bind")
    public R bind(
            @ApiParam("绑定数据")
            @RequestBody
                    UserBindVO userBindVO, HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        String formStr = userBindService.commitBindUser(userBindVO, userId);
        return R.ok().data("formStr", formStr);
    }

    @ApiOperation("账户绑定回调")
    @PostMapping("/notify")
    public String notify(HttpServletRequest request){
        log.info("调用notfy");
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = RequestHelper.switchMap(parameterMap);
        boolean signEquals = RequestHelper.isSignEquals(paramMap);
       if (!signEquals){
           return "fail";
       }
        userBindService.notify(paramMap);
        return "success";

    }


}

